package edu.cmu.ece.ds.droidchat.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.cmu.ece.ds.droidchat.R;

/**
 * Action item, displayed as menu with icon and text.
 *
 * Hengliang added a constructor.
 *
 * @author Lorensius. W. L. T
 * @author Hengliang Zhang
 *
 */
public class ActionItem {
    private static final String TAG = "ActionItem";
    public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
    public static final int VERTICAL = LinearLayout.VERTICAL;
    private Drawable icon;
    private String title;
    private LayoutInflater inflater;
    private View itemView;
    private int mOrientation;

    /**
     * Constructor
     */
    public ActionItem(Context context) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * Create an action item
     *
     * @param context
     *            the context needed to instantiate the action item
     * @param icon
     *            {@link Drawable} action item icon
     * @param title
     *            action item title
     * @param actionItemLayoutResId
     *            the layout for this ActionItem.
     */
    public ActionItem(Context context, int iconResId, int titleResId,
            int orientation) {
        if (iconResId != 0) {
            this.icon = context.getResources().getDrawable(iconResId);
        }
        if (titleResId != 0) {
            this.title = context.getResources().getString(titleResId);
        }

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = null;
        mOrientation = orientation;

    }

    public ActionItem(Context context, String title, int orientation) {
        this.title = title;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mOrientation = orientation;
    }

    /**
     * Constructor
     *
     * @param icon
     *            {@link Drawable} action icon
     */
    public ActionItem(Context context, Drawable icon, int orientation) {
        this.icon = icon;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mOrientation = orientation;
    }

    /**
     * Set action title
     *
     * @param title
     *            action title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Get action title
     *
     * @return action title
     */
    public String getTitle() {
        return this.title;
    }

    /**
     * Set action icon
     *
     * @param icon
     *            {@link Drawable} action icon
     */
    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    /**
     * Get action icon
     *
     * @return {@link Drawable} action icon
     */
    public Drawable getIcon() {
        return this.icon;
    }

    /**
     * Get action item {@link View}
     *
     * @param track
     *            the root view the action item will attach to
     *
     * @return action item {@link View}
     */
    public View getView(ViewGroup root) {
        if (itemView == null) {
            if (mOrientation == VERTICAL) {
                itemView = inflater.inflate(R.layout.action_item_vertical,
                        root, false);
            } else if (mOrientation == HORIZONTAL) {
                itemView = inflater.inflate(R.layout.action_item_horizontal,
                        root, false);
            } else {
                throw new InflateException();
            }
            ImageView img = (ImageView) itemView.findViewById(R.id.icon);
            TextView text = (TextView) itemView.findViewById(R.id.title);

            Log.d(TAG, "icon=" + icon);
            Log.d(TAG, "text=" + title);
            if (icon != null) {
                img.setImageDrawable(icon);
            }

            if (title != null) {
                text.setText(title);
            }
        }

        return itemView;
    }
}
